package cn.geracomium.util.web.upload;
import java.io.File;
import java.util.Arrays;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

public class FdfsUtil {
    /**
     * Controller层文件上传
     * @param file
     * @return
     * @throws Exception
     */
    public static String[] upload(MultipartFile file) throws Exception {
        String ext = file.getContentType().split("/")[1];
        ClassPathResource classPathResource = new ClassPathResource("config/fastdfs.properties");
        ClientGlobal.init(classPathResource.getClassLoader().getResource("config/fastdfs.properties").getPath());
        TrackerClient client = new TrackerClient();
        TrackerServer trackerServer = client.getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);
        NameValuePair[] metaList = new NameValuePair[3];
        metaList[0] = new NameValuePair("fileName", file.getName());
        metaList[1] = new NameValuePair("fileExtName", ext);
        metaList[2] = new NameValuePair("fileLength", String.valueOf(file.getSize()));
        String[] upload_file = storageClient.upload_file(file.getBytes(), ext, metaList) ;
        trackerServer.close();
        return upload_file ;
    }
}
